#!/usr/bin/env ruby

# The CallbackCollection class is used to define and execute custom _callbacks_, 
# i.e. portions of custom code to be executed in certain key points of RedBook's core classes
# (normally used in plugins).
class RedBook::CallbackCollection < Hash
		
		# Execute code associated to the _index_ hook with _args_ (used internally inside core classes).
		def exec(index, args={})
			if self[index] then
				self[index].children.each do |c|
					return true if c.value.call(args) == true # If the callback returns true, stop executing callbacks
				end
			end
			return false
		end
		
		# Create a new _index_ callback.
		# _proc_ corresponds to the actual implementation of the callback.
		def add(index, &callback_code)
			unless self[index]
				self[index] = {index => nil}
			end
			self[index] << {:callback => callback_code}
		end
	
end
